﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Handrise.CDCable.Mobile.Business;
using HandRise.MBHOP.Controls;

namespace Handrise.CDCable.Mobile.UI
{
    public partial class FrmPlanDeviceSelect : FrmBase
    {
        private Plan plan;
        private PlanDeviceService planDeviceService = new PlanDeviceService();
        private ScanService scanService;

        public FrmPlanDeviceSelect()
        {
            InitializeComponent();

            ttlbrhead.Text = "巡视设备查找";
            ilstbcup.Images.Add(ResourceImage.确定01);
            ilstbcup.Images.Add(ResourceImage.键盘01);
            ilstbcup.Images.Add(ResourceImage.返回01);
            ilstdown.Images.Add(ResourceImage.确定02);
            ilstdown.Images.Add(ResourceImage.键盘02);
            ilstdown.Images.Add(ResourceImage.返回02);
            btnSelectDeviceType.UpImage = ResourceImage.设备类型01;
            btnSelectDeviceType.DownImage = ResourceImage.设备类型02;
            ibtnscanner.UpImage = ResourceImage.使用扫描仪01;
            ibtnscanner.DownImage = ResourceImage.使用扫描仪02;
        }

        private void FrmPlanDeviceSelect_Load(object sender, EventArgs e)
        {
            scanService = new ScanService(this);
            scanService.OnReadCode += scanService_OnReadCode;


            cbWorkType.SelectedIndex = 0;

            if (IsReturn)
                LoadCacheData();
            else
                LoadData();

            tbPlanTitle.Text = plan.Title;
            
        }

        private void FrmPlanDeviceSelect_Closed(object sender, EventArgs e)
        {
            scanService.Close();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            if (Parameter.Count == 1)
            {
                plan = (Plan)Parameter[0];
            }
            else 
            {
                tbDeviceType.Text = (string)Parameter[0];

                //如果是从设备类型选择返回，需要读取之前的信息
                object[] oldParameters = (object[])(Parameter[1]);
                plan = (Plan)oldParameters[0];
                tbDeviceName.Text = (string)oldParameters[1];
                tbDeviceCode.Text = (string)oldParameters[3];
                cbWorkType.SelectedIndex = (int)oldParameters[4];
            }
        }

        /// <summary>
        /// 加载缓存数据
        /// </summary>
        private void LoadCacheData()
        {
            plan = (Plan)Caches[0];
            tbDeviceName.Text = (string)Caches[1];
            tbDeviceType.Text = (string)Caches[2];
            tbDeviceCode.Text = (string)Caches[3];
            cbWorkType.SelectedIndex = (int)Caches[4];
        }

        void scanService_OnReadCode(string code)
        {
            tbDeviceCode.Text = code;
            QueryPlanDevice();
        }

        private void btnSelectDeviceType_Click(object sender, EventArgs e)
        {
            FormManage.GotoNext("FrmTaskExecDetail", null, null, true);
        }

        private void bcOperate_OnButtonClick(object sender, int buttonIndex)
        {
            switch (buttonIndex)
            {

                case 0: //确定
                    QueryPlanDevice();
                    break;
                case 1: //键盘
                    InputMethod.SHFullScreen(this.Handle, 4);
                    InputMethod.SipShowIM(1);
                    tbDeviceName.Focus();
                    break;
                case 2: //返回
                    FormManage.GotoNext("FrmTaskExecDetail", null, null, true);
                    break;
            }
        }

        private void QueryPlanDevice()
        {
            List<PlanDevice> planDevices = planDeviceService.GetAlreadyPatrolPlanDevice(plan.PlanID, tbDeviceCode.Text, tbDeviceName.Text, tbDeviceType.Text);

            if (planDevices.Count == 0)
            {
                mssdlgComfirm.ShowDialog(this, "没有符合条件的巡视设备", DialogType.Confirm, FormManage.form_Deactivate);
                return;
            }

            if (planDevices.Count > 1)
                FormManage.GotoNext("FrmPlanDeviceList", new object[] { planDevices, cbWorkType.Text }, new object[] { plan, tbDeviceName.Text, tbDeviceType.Text, tbDeviceCode.Text, cbWorkType.SelectedIndex }, false);
            else if(planDevices.Count == 1)
            {
                FormManage.GotoNext("FrmPatrolPoint", new object[] { "FrmPlanDeviceSelect", planDevices[0], cbWorkType.Text }, new object[] { plan, tbDeviceName.Text, tbDeviceType.Text, tbDeviceCode.Text, cbWorkType.SelectedIndex }, false);
            }
        }



        private void ibtnscanner_Click(object sender, EventArgs e)
        {
            scanService.Scan();
        }

        private void btnSelectDeviceType_Click_1(object sender, EventArgs e)
        {
            FormManage.GotoNext("FrmEquipLst", new object[] { "FrmPlanDeviceSelect", new object[]{plan, tbDeviceName.Text, tbDeviceType.Text, tbDeviceCode.Text,cbWorkType.SelectedIndex }}, new object[] { plan, tbDeviceName.Text, tbDeviceType.Text, tbDeviceCode.Text, cbWorkType.SelectedIndex }, false);
            
        }

        private void FrmPlanDeviceSelect_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "194")
            {
                scanService.Scan();
            }
        }




    }
}